package com.cyz.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.cyz.dao.impl.ProductDaoImpl;
import com.cyz.dao.impl.ProducttypeDaoImpl;
import com.cyz.pojo.Orders;
import com.cyz.pojo.Producttype;
import com.cyz.service.OrdersService;
import com.cyz.service.ProductService;
import com.cyz.service.ShopcartService;
import com.cyz.service.UserService;
import com.cyz.service.impl.OrdersServiceImpl;
import com.cyz.service.impl.ProductServiceImpl;
import com.cyz.service.impl.ShopcartServiceImpl;
import com.cyz.service.impl.UserServiceImpl;

@Controller
@RequestMapping("/Shopcart")
public class Shopcart {
	
	@RequestMapping("/shopcart")
	public ModelAndView category(HttpServletRequest request,HttpServletResponse response) throws SQLException {
		ModelAndView mv = new ModelAndView();
		ShopcartService shopcartService = new ShopcartServiceImpl();
		List<com.cyz.pojo.Shopcart> shopcartList = shopcartService.getShopcartByUseId(1);//test
		for(com.cyz.pojo.Shopcart shopcart:shopcartList) {
			//System.out.println(shopcart.getProduct().getProductName());
		}
		mv.addObject("shopcartList",shopcartList);
		
		mv.addObject("prePage",request.getSession().getAttribute("nowPage"));
		mv.setViewName("shopcart");
		return mv;
	}
	
	/**
	  *  ��ӹ��ﳵ
	 * @param request
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping("/addShopcart")
	public void addShopcart(HttpServletRequest request,HttpServletResponse response) throws Exception {
		PrintWriter printWriter = response.getWriter();
		String userName=(String) request.getSession().getAttribute("userName");
		UserService userService=new UserServiceImpl();
		Integer userId=userService.getUserIdByUserName(userName);//获取用户id
	//	String productName1 = (String) request.getSession().getAttribute("productName");//获取产品名字
		String productName1 = request.getParameter("productName");//获取产品名字
		Integer amount = Integer.parseInt(request.getParameter("amount"));//获取产品数量
		com.cyz.pojo.Product product=new com.cyz.pojo.Product();
		ProductService productservice=new ProductServiceImpl();
		product=productservice.getProductByProductName(productName1);
		ShopcartService shopcartService =new ShopcartServiceImpl();
		shopcartService.insertShopcart(userId, product.getId(), amount,product.getPrice());
		printWriter.write("{\"res\":\"1\"}");
		printWriter.close();
	}
	
	/**
	  * 
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	@RequestMapping("/deleteShopcart")
	public void deleteShopcart(HttpServletRequest request,HttpServletResponse response) throws IOException{
		System.out.println("--------------------------------------------");
		PrintWriter printWriter =  response.getWriter();		
		String productName = request.getParameter("productName");
		String userName=(String) request.getSession().getAttribute("userName");
		ShopcartService shopcartService=new ShopcartServiceImpl();
		shopcartService.deteleShopcart(userName, productName);
		
		System.out.println(userName);
		printWriter.write("{\"res\":\"1\"}");
		printWriter.close();
	}
	/**
	  * 更新购买数量
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	@RequestMapping("/updateAmount")
	public void updateAmount(HttpServletRequest request,HttpServletResponse response) throws IOException{
		PrintWriter printWriter =  response.getWriter();		
		String productName = request.getParameter("productName");//获取产品ID
		ProductService productservice=new ProductServiceImpl();
		Integer productId=productservice.getProductIdByName(productName);
		System.out.println("product:"+productId);
		String userName=(String) request.getSession().getAttribute("userName");
		UserService userService=new UserServiceImpl();
		Integer userId=userService.getUserIdByUserName(userName);//获取用户id
		System.out.println("user:"+userId);
		Integer amount = Integer.parseInt(request.getParameter("amount"));//获取产品数量
		System.out.println("amount:"+amount);
		ShopcartService shopcartService =new ShopcartServiceImpl();
		shopcartService.updateShopcart(userId, productId, amount);
		printWriter.write("{\"res\":\"1\"}");
		printWriter.close();
	}
	/**
	  * 结算购物车所有东西，清空购物车
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws SQLException 
	 */
	@RequestMapping("/settleAllAccount")
	public void  settleAllAccount(HttpServletRequest request,HttpServletResponse response) throws IOException, SQLException{
		
		PrintWriter printWriter =  response.getWriter();		
		Date date = new Date();//获取当前时间
		SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd:hh:mm:ss");
		dateFormat.format(date);
		String userName=(String) request.getSession().getAttribute("userName");
		UserService userService=new UserServiceImpl();
		Integer userId=userService.getUserIdByUserName(userName);//获取用户id
		//System.out.println("user:"+userId);
		List<com.cyz.pojo.Shopcart> shopcartList=new ArrayList<com.cyz.pojo.Shopcart>();
		ShopcartService shopcartService =new ShopcartServiceImpl();
		shopcartList=shopcartService.getShopcartByUseId(userId);
		OrdersService orderService =new OrdersServiceImpl();
		for(com.cyz.pojo.Shopcart shopcart:shopcartList)
		{
			Orders orders=new Orders();
			orders.setOrderTime(date);
			orders.setAmount(shopcart.getAmount());
			orders.setUserId(userId);
			orders.setProductId(shopcart.getProductId());
			orders.setSumPrice(shopcart.getPrice()*shopcart.getAmount());
		
		    orderService.addOrder(orders);
		}
		 shopcartService.deteleAllShopcart(userId);
		printWriter.write("{\"res\":\"1\"}");
		printWriter.close();
	}
	
	
}
